<?php
	
	class Ruas_jalan extends arfaController
	{
		function __construct()
		{
			parent::__construct();
			$this->load->model("ruas_jalan_model");
			$this->load->model("titik_jalan_model");
			$this->load->model("master_periode_model");
			$this->load->model("master_upload_model");
		}
		public function form_upload_rj()
		{
			$this->form_validation->set_rules("periode","Periode","trim|required|callback_cek_periode");
			//$this->form_validation->set_rules("userfile","Data Upload","required");
			
			if($this->form_validation->run() == FALSE)
			{
				$data["judul"]			= "Ruas Jalan";
				$data["post_action"]	= "ruas_jalan/form_upload_rj";
				$data["data_periode"]	= $this->master_periode_model->get();
				$data["content"]		= $this->load->view("v_form_upload_rj",$data,true);
				$this->load->view("main",$data);
			}
			else
			{
				error_reporting(E_ALL ^ E_NOTICE);
				error_reporting(0);
				require_once 'include/excel_reader.php';
				$get_periode	= $this->input->post("periode");
				$data = new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
				foreach($data->dump(false,false,false,false) as $row)
				{
					$nama_ruas		= $row["NAMA_RUAS"];
					$cek			= str_split($nama_ruas);
					$ruas			= "";
					
					if($cek[0] == " ")
					{
						$ruas = ltrim($nama_ruas," ");
					}
					else
					{
						$ruas = $nama_ruas;
					}
					
					$data2	= array(
						"ID_PERIODE"		=> $get_periode,
						"NAMA_KOTA"			=> $row["NAMA_KOTA"],
						"NO_RUAS1"			=> $row["NO_RUAS1"],
						"NO_RUAS2"			=> $row["NO_RUAS2"],
						"NO_RUAS3"			=> $row["NO_RUAS3"],
						"NAMA_RUAS"			=> $ruas,
						"PANJANG"			=> $row["PANJANG"],
						"HOTMIX_BAIK"		=> $row["HOTMIX_BAIK"],
						"HOTMIX_SEDANG"		=> $row["HOTMIX_SEDANG"],
						"HOTMIX_RR"			=> $row["HOTMIX_RR"],
						"HOTMIX_RB"			=> $row["HOTMIX_RB"],
						"LAPEN_BAIK"		=> $row["LAPEN_BAIK"],
						"LAPEN_SEDANG"		=> $row["LAPEN_SEDANG"],
						"LAPEN_RR"			=> $row["LAPEN_RR"],
						"LAPEN_RB"			=> $row["LAPEN_RB"],
						"LPC"				=> $row["LPC"],
						"TANAH"				=> $row["TANAH"],
						"HUTAN"				=> $row["HUTAN"],
						"PULAU"				=> $row["PULAU"],
						"RCI"				=> $row["RCI"],
						"IRI"				=> $row["IRI"]
					);	
						
					if($data2["NAMA_KOTA"] != NULL){
						$this->ruas_jalan_model->save($data2);
					}
					
					$id_ruas 	= $this->db->insert_id();
					$latlong	= $row["TITIK_RUTE"];
					$group 		= explode(";",$latlong);
					$i			= 0;
					foreach($group as $row)
					{
						$result = $group[$i++];
						$column = explode(",",$result);
						
						$cek 	= str_split($column[0]);
						$cek2 	= str_split($column[1]);
						
						//cek kondisi latitude
						if($cek[0] == "-")
						{
							if(is_float($column[0]) || is_numeric($column[0]) && ((float) $column[0] != (int) $column[0])) {
								$lat = $column[0];
							} else {
								$totallat = explode(".",$column[0]);	
								$combine1 = $totallat[1].".".$totallat[2];
								$lat = $totallat[0]-(($combine1*60)/(60*60));
							}
						}
						elseif($cek[0] == " ")
						{
							$columns=ltrim($column[0]," ");
							$cek_la = str_split($columns);
							if($cek_la[0] == "-")
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$lat = $columns;
								} else {
									$totallat = explode(".",$columns);	
									$combine1 = $totallat[1].".".$totallat[2];
									$lat = $totallat[0]-(($combine1*60)/(60*60));
								}
							}
							else
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$lat = $columns;
								} else {
									$totallat = explode(".",$columns);	
									$combine1 = $totallat[1].".".$totallat[2];
									$lat = $totallat[0]+(($combine1*60)/(60*60));
								}
							}
						}
						elseif($cek[0] == "'")
						{
							$columns = ltrim($column[0],"'");
							$cek_la = str_split($columns);
							if($cek_la[0] == "-")
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$lat = $columns;
								} else {
									$totallat = explode(".",$columns);	
									$combine1 = $totallat[1].".".$totallat[2];
									$lat = $totallat[0]-(($combine1*60)/(60*60));
								}
							}
							else
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$lat = $columns;
								} else {
									$totallat = explode(".",$columns);	
									$combine1 = $totallat[1].".".$totallat[2];
									$lat = $totallat[0]+(($combine1*60)/(60*60));
								}
							}
						}
						else
						{
							if(is_float($column[0]) || is_numeric($column[0]) && ((float) $column[0] != (int) $column[0])) {
								$lat = $column[0];
							} else {
								$totallat = explode(".",$column[0]);	
								$combine1 = $totallat[1].".".$totallat[2];
								$lat = $totallat[0]+(($combine1*60)/(60*60));
							}
						}
						
						
						//cek kondisi longtitude
						if($cek2[0] == "-")
						{
							if(is_float($column[1]) || is_numeric($column[1]) && ((float) $column[1] != (int) $column[1])) {
								$long = $column[1];
							} else {
								$totallong = explode(".",$column[0]);	
								$combine2 = $totallong[1].".".$totallong[2];
								$long = $totallong[0]-(($combine2*60)/(60*60));
							}
						}
						elseif($cek2[0] == " ")
						{
							$columns=ltrim($column[1]," ");
							$cek_lo = str_split($columns);
							if($cek_lo[0] == "-")
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$long = $column[1];
								} else {
									$totallong = explode(".",$columns);	
									$combine2 = $totallong[1].".".$totallong[2];
									$long = $totallong[0]-(($combine2*60)/(60*60));
								}
							}
							else
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$long = $columns;
								} else {
									$totallong = explode(".",$columns);	
									$combine2 = $totallong[1].".".$totallong[2];
									$long = $totallong[0]+(($combine2*60)/(60*60));
								}
							}
							
						}
						elseif($cek2[0] == "'")
						{
							$columns=ltrim($column[1],"'");
							$cek_lo = str_split($columns);
							if($cek_lo[0] == "-")
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$long = $column[1];
								} else {
									$totallong = explode(".",$columns);	
									$combine2 = $totallong[1].".".$totallong[2];
									$long = $totallong[0]-(($combine2*60)/(60*60));
								}
							}
							else
							{
								if(is_float($columns) || is_numeric($columns) && ((float) $columns != (int) $columns)) {
									$long = $columns;
								} else {
									$totallong = explode(".",$columns);	
									$combine2 = $totallong[1].".".$totallong[2];
									$long = $totallong[0]+(($combine2*60)/(60*60));
								}
							}
						}
						else
						{
							if(is_float($column[1]) || is_numeric($column[1]) && ((float) $column[1] != (int) $column[1])) {
								$long = $column[1];
							} else {
								$totallong = explode(".",$column[1]);	
								$combine2 = $totallong[1].".".$totallong[2];
								$long = $totallong[0]+(($combine2*60)/(60*60));
							}
						}
						
						$data2	= array(
							"ID_RUAS"			=> $id_ruas,
							"LATITUDE"			=> $lat,
							"LONGTITUDE"		=> $long
							);	
					
						if($data2["ID_RUAS"] != NULL || $data2["ID_RUAS"] != 0 || $data2["ID_RUAS"] != "")
						{
							$this->titik_jalan_model->save($data2);
						}
					}
				}
				echo "
					<script>
						alert('Data berhasil disimpan');
						document.location='';
					</script>
				";
			}
		}
		
		public function cek_periode($periode)
		{
			$is_exist 	= $this->ruas_jalan_model->cek_periode($periode);
			
			if ($is_exist) {
				// $this->form_validation->set_message("cek_periode", "Duplikat periode tidak diperbolehkan !");
				$this->ruas_jalan_model->hapus_periode($periode);
				return TRUE;
			}
		}
		
		public function grid_rj($periode = NULL)
		{
			if(!isset($periode))
			{
				$data["judul"]			= "Ruas Jalan";
				$data["periode"]		= $this->master_periode_model->get();
				$data["data_rj"]		= $this->ruas_jalan_model->get_data();
				$data["content"]		= $this->load->view("v_grid_rj",$data,true);
				$this->load->view("main",$data);
			}
			else
			{
				if($periode == 99999999)
				{
					$data["judul"]			= "Ruas Jalan";
					$data["id"]				= $periode;
					$data["periode"]		= $this->master_periode_model->get();
					$data["data_rj"]		= $this->ruas_jalan_model->get_paktif();
					$data["content"]		= $this->load->view("v_grid_rj",$data,true);
					$this->load->view("main",$data);
				}
				elseif($periode == 0)
				{
					$data["judul"]			= "Ruas Jalan";
					$data["id"]				= $periode;
					$data["periode"]		= $this->master_periode_model->get();
					$data["data_rj"]		= $this->ruas_jalan_model->get_data();
					$data["content"]		= $this->load->view("v_grid_rj",$data,true);
					$this->load->view("main",$data);
				}
				else
				{
					$data["judul"]			= "Ruas Jalan";
					$id						= $periode;
					$data["id"]				= $periode;
					$data["selected_periode"] = $this->master_periode_model->get($id);
					$data["periode"]		= $this->master_periode_model->get();
					$data["data_rj"]		= $this->ruas_jalan_model->get_data_periode($periode);
					$data["content"]		= $this->load->view("v_grid_rj",$data,true);
					$this->load->view("main",$data);
				}
			}
		}
		
		public function form_rj($id)
		{
			$this->form_validation->set_rules("nama_kota","Nama Kota","reuqired");
			
			if($this->form_validation->run() == FALSE)
			{
				$data["judul"]			= "Ruas Jalan";
				$data["id"]				= $id;
				$data["post_action"]	= "ruas_jalan/form_rj/".$id;
				$data["data_rj"]		= $this->ruas_jalan_model->get($id);
				$data["content"]		= $this->load->view("v_form_rj",$data,true);
				$this->load->view("main",$data);
			}
			else
			{
				$get_kota		= $this->input->post("nama_kota");
				$get_ruas1		= $this->input->post("ruas1");
				$get_ruas2		= $this->input->post("ruas2");
				$get_ruas3		= $this->input->post("ruas3");
				$get_nama_r		= $this->input->post("nama_ruas");
				$get_panjang	= $this->input->post("panjang");
				$get_hotmix_b	= $this->input->post("hotmix_baik");
				$get_hotmix_s	= $this->input->post("hotmix_sedang");
				$get_hotmix_rr	= $this->input->post("hotmix_rr");
				$get_hotmix_rb	= $this->input->post("hotmix_rb");
				$get_lapen_b	= $this->input->post("lapen_baik");
				$get_lapen_s	= $this->input->post("lapen_sedang");
				$get_lapen_rr	= $this->input->post("lapen_rr");
				$get_lapen_rb	= $this->input->post("lapen_rb");
				$get_lpc		= $this->input->post("lpc");
				$get_tanah		= $this->input->post("tanah");
				$get_hutan		= $this->input->post("hutan");
				$get_pulau		= $this->input->post("pulau");
				$get_rci		= $this->input->post("rci");
				$get_iri		= $this->input->post("iri");

				
				$data = array(
					"NAMA_KOTA"			=> $get_kota,
					"NO_RUAS1"			=> $get_ruas1,
					"NO_RUAS2"			=> $get_ruas2,
					"NO_RUAS3"			=> $get_ruas3,
					"NAMA_RUAS"			=> $get_nama_r,
					"PANJANG"			=> $get_panjang,
					"HOTMIX_BAIK"		=> $get_hotmix_b,
					"HOTMIX_SEDANG"		=> $get_hotmix_s,
					"HOTMIX_RR"			=> $get_hotmix_rr,
					"HOTMIX_RB"			=> $get_hotmix_rb,
					"LAPEN_BAIK"		=> $get_lapen_b,
					"LAPEN_SEDANG"		=> $get_lapen_s,
					"LAPEN_RR"			=> $get_lapen_rr,
					"LAPEN_RB"			=> $get_lapen_rb,
					"LPC"				=> $get_lpc,
					"TANAH"				=> $get_tanah,
					"HUTAN"				=> $get_hutan,
					"PULAU"				=> $get_pulau,
					"RCI"				=> $get_rci,
					"IRI"				=> $get_iri
				);
				
				$this->ruas_jalan_model->save($data,$id);
				
				echo "
					<script>
						alert('Data ruas jalan berhasil dirubah !');
						document.location = '".site_url('ruas_jalan/grid_rj')."'
					</script>
				";
			}
		}
		
		public function data_json()
		{
			$data	= $this->ruas_jalan_model->get_paktif();
			echo json_encode($data);
		}
		
		public function titik_data_json()
		{
			$data = $this->titik_jalan_model->get_paktif();
			
			echo json_encode($data);
		}
		
		public function image_data_json()
		{
			$data = $this->master_upload_model->get_paktif();
			echo json_encode($data);
		}
		
		public function lihat_rute($id = NULL)
		{
			$data["ruas"]			= $this->ruas_jalan_model->get($id);
			$data["titik"]			= $this->titik_jalan_model->get_rute($id);
			$data["content"]		= $this->load->view("v_titik_rute",$data,true);
			$this->load->view("main_peta",$data);
		}
	}
